package com.cn.wanxi.servlet.products;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IProductsService;
import com.cn.wanxi.service.impl.ProductsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/producat/producatsFindll")
public class productsFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.处理乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        //前台拿值
        String pageNum=req.getParameter("pageNum");
        String pageSize=req.getParameter("pageSize");


        ProductModel productModel=new ProductModel();
        //封装
        PageModel pageModel=new PageModel();
        pageModel.setPageNum(pageNum==null || "".equals(pageNum)? 1 :Integer.parseInt(pageNum));
        pageModel.setPageSize(pageSize==null || "".equals(pageSize) ? 10:Integer.parseInt(pageSize));

        //调用服务逻辑层
        IProductsService iProductsService =new ProductsServiceImpl();


        //返回数据
        List<ProductModel> list=iProductsService.findAll(pageModel);
        int number=iProductsService.getCount(productModel);

        //封装json
        //6.将数据转发到页面
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("number",number);
        resp.getWriter().println(jsonObject);
    }
}
